<?php
require("./include/fpdf/fpdf.php");
include "_fpdf_mc_table.php";

class PPDF extends PDF
{
	function Header()
	{
		global $header_0, $w_0, $header, $w, $title;
		//Logo
	    $this->Image('./images/logo-irou.jpg',20,8,15);
	    //Arial bold 15
	    $this->SetFont('Arial','B',13);
	    //Move to the right
	    //$this->Cell(80);
	    //Title
	    $this->Cell(0,10,$title,0,0,'C');
	    //Line break
	    $this->Ln(15);
	    
	    // If table is needed 
	    if($header !=''){
		    //Colors, line width and bold font
		    $this->SetFillColor(255,0,0);
		    $this->SetTextColor(255);
		    $this->SetDrawColor(128,0,0);
		    $this->SetLineWidth(.3);
		    $this->SetFont('Arial','B',8);
		    //Header
	        //$this->Cell(10,10,'Bil.',1,0,'C',true);
		    // Heaer Bulan
			for($i=0;$i<count($header_0);$i++)
		        $this->Cell($w_0[$i],7,preg_replace("/\d|_/","",$header_0[$i]),1,0,'C',true);
		    $this->Ln();
			// Header tajuk 
		    for($i=0;$i<count($header);$i++)
		        $this->Cell($w[$i],7,preg_replace("/\d|:/","",$header[$i]),1,0,'C',true);
		    $this->Ln();
	    }
	}
}

//Instanciation of inherited class
$pdf=new PPDF();
$pdf->AliasNbPages();
$pdf->AddPage('L');
$pdf->SetFont('Times','',8);
$pdf->SetWidths($w);


// Set the fields from DB == table header ....
$fields = $header;
//print_r($fields);
$i=0;
$c=1;
$data = array();
// fetch all data into array ...
while($d = $res->FetchNextObject()){
$i++;
	$x = 0;
	$data = array();
	$data[] = $c;
	$calc_arr = array();
	
	$no_ahli = $d->NO_AHLI;
	
	// loop tru each fields ... 
	for($z=1; $z<count($fields); $z++){
		if(!preg_match("/\d/",$fields[$z])){
			$data[] = ucwords($d->$fields[$z]);
		}else{
			list($mnth, $abc) = split(':', $fields[$z]);
			
			// tunggakan sehingga bulan ini: < this month
			if($abc == 'B') { $calc_arr['get_tunggakan_ahli'] = get_tunggakan_ahli($no_ahli, $tahun='2011', $mnth); }			
			
			// Yuran wajib bulan ini : $WAJIB_BAYAR DLM config ...
			if($abc == 'C') { $calc_arr['wajib_bayar'] = $WAJIB_BAYAR; }
			
			// Yuran yang sepatutnya kena bayar ... bayaran wajib + tunggakan ??
			if($abc == 'E') { $calc_arr['get_sudah_dibayar'] = get_sudah_dibayar($no_ahli, $tahun='2011', $mnth); }
			
			switch($abc){
				// Yuran Ahli
				case 'A':
					$data[] = '-';
				break;
				
				// Baki bulan sblm yg blm bayo ....
				case 'B':
					//echo $mnth .'->'. $calc_arr['get_tunggakan_ahli']."<br>";
					$data[] = $calc_arr['get_tunggakan_ahli'];
				break;
				
				// Yuran
				case 'C':
					$data[] = $calc_arr['wajib_bayar'];
				break;
				
				// Yuram mesti bayar
				case 'D':
					$data[] = $calc_arr['get_tunggakan_ahli'] + $calc_arr['wajib_bayar'];
				break;
				
				// Jumlah sudah bayar
				case 'E':
					$data[] = $calc_arr['get_sudah_dibayar'];
				break;

				// Baki misti bayar:
				case 'F':
					$data[] = ($calc_arr['wajib_bayar'] - get_sudah_dibayar($no_ahli, $tahun, $mnth) ) + $calc_arr['get_tunggakan_ahli'];
				break;
			}
		}
	}
	$pdf->row($data, $fill, true);
	$fill=!$fill;
	$c++;
}
$pdf->Output();
?>
